package com.config.center.database.controller;

import com.alibaba.druid.pool.DruidDataSource;
import com.config.center.database.config.DynamicDataSource;
import com.config.center.database.pojo.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname UserController
 * @Description TODO
 * @Date 2020/12/12 23:56
 * @Created by wpp25
 */
@RestController
@RequestMapping("/api/user")
public class UserController {

//    @Resource
//    private UserDao userDao;

    @Resource
    private DynamicDataSource dynamicDataSource;

    @GetMapping("/findAll")
    public List<User> findAll() {
        DruidDataSource dataSource = dynamicDataSource.getDataSource();

//        List<User> list = userDao.findAll();
        try {
            Connection connection = dataSource.getConnection();
            ResultSet resultSet = connection.prepareStatement("select * from user").executeQuery();
            while (resultSet.next()) {
                System.out.printf("username %s %n",resultSet.getString(2));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
//        System.out.println(list);
        return new ArrayList<>();
    }

}
